-- Tags: no-parallel, no-fasttest

SET output_format_pretty_single_large_number_tip_threshold = 0;

{# this test checks backward compatibility of aggregate functions States against IPv4, IPv6 types #}

{% set ip4_generator = "select num::UInt32::IPv4 ip from (select arrayJoin(range(999999999, number)) as num from numbers(999999999,50)) order by ip" %}
{% set ip6_generator = "SELECT toIPv6(IPv6NumToString(toFixedString(reinterpretAsFixedString(num)||reinterpretAsFixedString(num), 16))) AS ip FROM (select arrayJoin(range(1010011101, number)) as num from numbers(1010011101,50)) order by ip" %}
{% set ip_generators = {'ip4': ip4_generator, 'ip6': ip6_generator} %}


{% set agg_func_list = [ "min", "max", "first_value", "last_value", "topK", "groupArray", "groupUniqArray", "uniq", "uniqExact", "uniqCombined", "uniqCombined64", "uniqHLL12", "uniqTheta" ] %}

{% for generator_name, ip_generator in ip_generators.items() %}

select '----- hash / State / {{ generator_name }} -----';
select
  {% for func in agg_func_list -%}
     cityHash64(hex( {{ func }}State(ip) )) AS {{ func }}State{{ "," if not loop.last }}
  {% endfor -%}
from ( {{ ip_generator }} ) format Vertical;

{% endfor -%}


{% for generator_name, ip_generator in ip_generators.items() %}

select '----- finalizeAggregation / State / {{ generator_name }} -----';
select
  {% for func in agg_func_list -%}
     finalizeAggregation( {{ func }}State(ip) ) AS {{ func }}{{ "," if not loop.last }}
  {% endfor -%}
from ( {{ ip_generator }} ) format Vertical;

{% endfor -%}


{% for generator_name, ip_generator in ip_generators.items() %}

select '----- hash / IfState / {{ generator_name }} -----';
select
  {% for func in agg_func_list -%}
     cityHash64(hex( {{ func }}IfState(ip, 1) )) AS {{ func }}IfState{{ "," if not loop.last }}
  {% endfor -%}
from ( {{ ip_generator }} ) format Vertical;

{% endfor -%}


{% for generator_name, ip_generator in ip_generators.items() %}

select '----- finalizeAggregation / IfState / {{ generator_name }} -----';
select
  {% for func in agg_func_list -%}
     finalizeAggregation( {{ func }}IfState(ip, 1) ) AS {{ func }}{{ "," if not loop.last }}
  {% endfor -%}
from ( {{ ip_generator }} ) format Vertical;

{% endfor -%}


{% set agg_func_list = [ "argMin", "argMax" ] %}

{% for generator_name, ip_generator in ip_generators.items() %}

select '----- Arg / hash / State / {{ generator_name }} -----';
select
  {% for func in agg_func_list -%}
     cityHash64(hex( {{ func }}State(ip, ip) )) AS {{ func }}State{{ "," if not loop.last }}
  {% endfor -%}
from ( {{ ip_generator }} ) format Vertical;

{% endfor -%}


{% for generator_name, ip_generator in ip_generators.items() %}

select '----- Arg / finalizeAggregation / State / {{ generator_name }} -----';
select
  {% for func in agg_func_list -%}
     finalizeAggregation( {{ func }}State(ip, ip) ) AS {{ func }}State{{ "," if not loop.last }}
  {% endfor -%}
from ( {{ ip_generator }} ) format Vertical;

{% endfor -%}




{# let's test functions with not deterministic result against 1 row, to make it deterministic #}
{% set ip4_generator = "select number::UInt32::IPv4 ip from numbers(999999999,1) order by ip" %}
{% set ip6_generator = "SELECT toIPv6(IPv6NumToString(toFixedString(reinterpretAsFixedString(number)||reinterpretAsFixedString(number), 16))) AS ip FROM numbers(1010011101, 1) order by ip" %}

{% set ip_generators = {'ip4': ip4_generator, 'ip6': ip6_generator} %}

{% set agg_func_list = [ "any", "anyHeavy", "anyLast" ] %}


{% for generator_name, ip_generator in ip_generators.items() %}

select '----- hash / State / {{ generator_name }} -----';
select
  {% for func in  agg_func_list  -%}
     cityHash64(hex( {{ func }}State(ip) )) AS {{ func }}State{{ "," if not loop.last }}
  {% endfor -%}
from ( {{ ip_generator }} ) format Vertical;

{% endfor -%}


{% for generator_name, ip_generator in ip_generators.items() %}

select '----- finalizeAggregation / State / {{ generator_name }} -----';
select
  {% for func in  agg_func_list  -%}
     finalizeAggregation( {{ func }}State(ip) ) AS {{ func }}{{ "," if not loop.last }}
  {% endfor -%}
from ( {{ ip_generator }} ) format Vertical;

{% endfor -%}



{% set agg_func_list = [ "sumMap", "minMap", "maxMap" ] %}

{% for generator_name, ip_generator in ip_generators.items() %}

select '----- Map/Map hash / State / {{ generator_name }} -----';
select
  {% for func in agg_func_list  -%}
     cityHash64(hex( {{ func }}State(map(ip, 1::Int64)) )) AS {{ func }}State{{ "," if not loop.last }}
  {% endfor -%}
from ( {{ ip_generator }} ) format Vertical;

{% endfor -%}



{% for generator_name, ip_generator in ip_generators.items() %}

select '----- Map/Map finalizeAggregation / State / {{ generator_name }} -----';
select
  {% for func in agg_func_list -%}
     finalizeAggregation( {{ func }}State(map(ip, 1::Int64)) ) AS {{ func }}{{ "," if not loop.last }}
  {% endfor -%}
from ( {{ ip_generator }} ) format Vertical;

{% endfor -%}


{% for generator_name, ip_generator in ip_generators.items() %}

select '----- Map/Array hash / State / {{ generator_name }} -----';
select
  {% for func in agg_func_list -%}
     cityHash64(hex( {{ func }}State([ip], [1::Int64]) )) AS {{ func }}State{{ "," if not loop.last }}
  {% endfor -%}
from ( {{ ip_generator }} ) format Vertical;

{% endfor -%}


{% for generator_name, ip_generator in ip_generators.items() %}

select '----- Map/Array finalizeAggregation / State / {{ generator_name }} -----';
select
  {% for func in agg_func_list -%}
     finalizeAggregation( {{ func }}State([ip], [1::Int64]) ) AS {{ func }}{{ "," if not loop.last }}
  {% endfor -%}
from ( {{ ip_generator }} ) format Vertical;

{% endfor -%}
